package com.soccertgc.utils
{
	import flash.utils.Dictionary;

	public class Utils
	{
		/**
		 * Metodo che permette di evitare la nidificazione di array quando si
		 * intende passare un parametro ...rest a un altro metodo che accetta
		 * parametri ...rest 
		 * @param rest
		 * @return 
		 */		
		public static function removeRestNesting(rest:Array):Array
		{
			var args:Array;
			if (rest.length == 1 && rest[0] is Array)
			{
				args = rest[0] as Array;
			}
			else
			{
				args = rest;
			}
			return args;
		}
		
		public static function dictionaryEquals(d1:Dictionary, d2:Dictionary):Boolean
		{
			// quick check for the same object
			if( d1 == d2 )
				return true;
			
			// check for null
			if( d1 == null || d2 == null )
				return false;
			
			// go through the keys in d1 and check if they're in d2 - also keep a count
			var count:int = 0;
			for( var key:* in d1 )
			{
				// check if the key exists
				if( !( key in d2 ) )
					return false;
				
				// check that the values are the same
				if( d1[key] != d2[key] )
					return false;
				
				count++;
			}
			
			// now just make sure d2 has the same number of keys
			var count2:int = 0;
			for( key in d2 )
				count2++;
			
			// return if they're the same size
			return ( count == count2 );			
		}
		
		public static function dictionarySize(d:Dictionary):int
		{
			var n:int = 0;
			var k:* = null;
			for (k in d) n++;
			return n;
		}
		
		public static function coalesce(data:Object):String
		{
			return data == null ? "<null>" : data.toString();
		}
	}
}